﻿Partial Public Class admin_auswertung
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            dropdownfuellenJahr()
            gridfuellen(Integer.Parse(dd_auswahlJahr.SelectedValue), chk_Intern.Checked)
            calenderfuellen()
            geleistete_PT()
        End If

    End Sub


    Private Sub dropdownfuellenJahr()

        Dim datumAktuelle As New Date
        datumAktuelle = Date.Now

        For i = 0 To 4


            dd_auswahlJahr.Items.Add((Integer.Parse(datumAktuelle.Year)) + (4 - i))

        Next

        For i = 0 To 4
            If i <> 0 Then
                dd_auswahlJahr.Items.Add((Integer.Parse(datumAktuelle.Year)) - i)
            End If
        Next
        dd_auswahlJahr.SelectedItem.Text = datumAktuelle.Year
    End Sub

    Private Sub calenderfuellen()
        TextBox1.Text = "01.01." + Date.Now.Year.ToString
        TextBox2.Text = "31.12." + Date.Now.Year.ToString
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_auswahlJahr.SelectedIndexChanged
        gridfuellen(Integer.Parse(dd_auswahlJahr.SelectedValue), chk_Intern.Checked)
    End Sub

    Private Sub gridfuellen(ByVal gewaehltesjahr As Integer, ByVal extern_intern As Boolean)
        Dim db As New hc_databaseDataContext
        Dim newdate As New Date
        Dim bla As String = "01.01." + gewaehltesjahr.ToString
        newdate = Date.Parse(bla)
        Dim var = From pr In db.Projekts, rp In db.Reports, mg In db.Mitgliede_Anwaerter _
                  Where pr.ID = rp.projektid And mg.ID = rp.Mitgliede_AnwaerterID And pr.Projekt_extern_intern = extern_intern And rp.Angenommen_pl = True _
                  And rp.Datum.Year = newdate.Year _
                  Group rp By mg.ID, mg.Vorname, mg.Nachname Into Group _
 Select ID, Vorname, Nachname, Geleistete_PT = Group.Sum(Function(rp) (rp.Dauer / 8)) Order By Geleistete_PT Descending


        gv_gearbeitet.DataSource = var
        gv_gearbeitet.DataBind()
    End Sub

    Protected Sub gv_gearbeitet_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gv_gearbeitet.SelectedIndexChanged

    End Sub

    Private Sub geleistete_PT()

        Dim extern_intern As Boolean
        If DropDownList1.Text = "externen" Then
            extern_intern = False
        Else : extern_intern = True
        End If

        Dim datum1 As New Date
        Dim datum2 As New Date
        Dim bla = TextBox2.Text.ToString
        datum1 = Date.Parse(TextBox1.Text)
        datum2 = Date.Parse(TextBox2.Text)


        Dim db As New hc_databaseDataContext
        Dim var = (From pr In db.Projekts, rp In db.Reports _
                  Where pr.ID = rp.projektid And pr.Projekt_extern_intern = extern_intern And rp.Angenommen_pl = True _
                  And rp.Datum >= datum1 And rp.Datum <= datum2 _
                  Select (rp.Dauer / 8)).Sum.ToString

        Label2.Text = var

    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
        geleistete_PT()
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox2.TextChanged
        geleistete_PT()
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged1(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged
        geleistete_PT()
    End Sub
End Class